#include "drv_dma.h"
#include "drv_usart.h"

extern uint32_t dma_src[10];
extern uint32_t dma_drc[10];

void drv_dma_init(uint32_t *src,uint32_t *drc, uint32_t size)//32位 4个字节 一个字节占8位
{
	DMA_InitTypeDef DMA_InitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);//内存到内存 只能用DMA2

	// 等待DMA可配置
    while(DMA_GetCmdStatus(DMA2_Stream1) != DISABLE);
	
	DMA_InitStruct.DMA_BufferSize=size;
	DMA_InitStruct.DMA_Channel=DMA_Channel_0;
	DMA_InitStruct.DMA_DIR=DMA_DIR_MemoryToMemory;
	DMA_InitStruct.DMA_FIFOMode=DMA_FIFOMode_Disable;
	DMA_InitStruct.DMA_Memory0BaseAddr=(uint32_t)drc;//目标地址
	DMA_InitStruct.DMA_MemoryBurst=DMA_MemoryBurst_Single;//一次1个数据单元传输
	DMA_InitStruct.DMA_MemoryDataSize=DMA_MemoryDataSize_Word;
	DMA_InitStruct.DMA_MemoryInc=DMA_MemoryInc_Enable;//是否自增
	DMA_InitStruct.DMA_Mode=DMA_Mode_Normal;
	
	DMA_InitStruct.DMA_PeripheralBaseAddr=(uint32_t)src;//数据源地址
	DMA_InitStruct.DMA_PeripheralBurst=DMA_PeripheralBurst_Single;
	DMA_InitStruct.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Word;
	DMA_InitStruct.DMA_PeripheralInc=DMA_PeripheralInc_Enable;
	DMA_InitStruct.DMA_Priority=DMA_Priority_VeryHigh;

	DMA_Init(DMA2_Stream1, &DMA_InitStruct);

	DMA_Cmd(DMA2_Stream1, ENABLE);
}

//校验
void drv_dma_compare(void)
{
	uint32_t i;
	for(i=0;i<10;i++)
	{
		if(dma_src[i]!=dma_drc[i])
		{
			printf("dma_src is error\r\n");
			return ;
		}
	}
	printf("ddma_src is success\r\n");
}
